//
//  MHOAuthController.swift
//  MHSinaWeiBo
//
//  Created by a on 15/10/6.
//  Copyright © 2015年 icast. All rights reserved.
//

import UIKit

class MHOAuthController: UIViewController,UIWebViewDelegate {

    let client_id :String = "1768113202"
    let redirect_uri :String = "http://baidu.com"
    
    
    //强制转换，将当前的view 转成 UIWebView
    var webView :UIWebView  {
        get{
            return view as! UIWebView
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置代理
        webView.delegate = self
        loadOAuth()
        
    }

    
    @IBAction func backBtn(sender: AnyObject) {
        dismissViewControllerAnimated(true, completion: nil)
    }
    private func loadOAuth(){
        
       
        
        let apiUrl: String = "https://api.weibo.com/oauth2/authorize?client_id="+client_id+"&redirect_uri="+redirect_uri
        
        let url = NSURL(string: apiUrl)
        //print(url)
        
        //加载url
        webView.loadRequest(NSURLRequest(URL:url!))
        
        
    }
    
    
    //MARK: 实现webView的代理方法
    func webViewDidFinishLoad(webView: UIWebView) {
        //title = "新浪微博"
        
        //利用js设置title
        let jsurl = "document.title"
        let webTitle = webView.stringByEvaluatingJavaScriptFromString(jsurl)
        
        title = webTitle
        
        
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    
    
}
